local mengyan = fk.CreateSkill{
  name = "sr__mengyan",
  tags = {Skill.Compulsory}
}

Fk:loadTranslationTable {
  ["sr__mengyan"] = "梦魇",
  [":sr__mengyan"] = "锁定技，其他角色对你造成伤害后，你令其增加1点体力下限；其他角色对你使用【桃】或【桃园结义】后，你令其增加1点体力上限。",
  ["@min_hp"] = "体力下限增加"
}

---获得体力下限
---@param player Player
local getMinHp = function (player)
  return player:getMark("@min_hp") + 1
end

---设置体力下限
---@param player ServerPlayer
---@param num integer?
local setMinHp = function (player, num)
  num = num or 1
  return player.room:setPlayerMark(player, "@min_hp", num - 1)
end

---增加体力下限
---@param player ServerPlayer
---@param num integer?
local addMinHp = function (player, num)
  num = num or 1
  return player.room:addPlayerMark(player, "@min_hp", num)
end

mengyan:addEffect(fk.Damage, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and target ~= player and data.to == player
  end,
  on_use = function (self, event, target, player, data)
    if getMinHp(target) < target.maxHp then
      player.room:addPlayerMark(target, "@min_hp", 1)
    end
  end
})

mengyan:addEffect(fk.MaxHpChanged, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(mengyan.name, true, true) and getMinHp(target) > target.maxHp
  end,
  on_use = function (self, event, target, player, data)
    setMinHp(target, target.maxHp)
  end
})

mengyan:addEffect(fk.AfterDying, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(mengyan.name, true, true) and target:getMark("_min_hp_lose") > 0
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    target.hp = target.hp + target:getMark("_min_hp_lose")
    room:broadcastProperty(target, "hp")
    room:setPlayerMark(target, "_min_hp_lose", 0)
  end
})

mengyan:addEffect(fk.BeforeHpChanged, {
  is_delay_effect = true,
  mute = true,
  priority = 0,
  can_trigger = function (self, event, target, player, data)
    if not (player:hasSkill(mengyan.name, true, true)  and getMinHp(target) > 1) then return end
    if not data.damageEvent then
      return data.num < 0 and (target.hp + data.num) < getMinHp(target)
    end
      return target.hp + target.shield - data.damageEvent.damage < getMinHp(target)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    target.hp = target.hp - (getMinHp(target) - 1)
    room:broadcastProperty(target, "hp")
    room:addPlayerMark(target, "_min_hp_lose", getMinHp(target) - 1)
  end
})

mengyan:addEffect(fk.CardUseFinished, {
  can_trigger = function (self, event, target, player, data)
    return target ~= player and table.contains(data.tos, player) and player:hasSkill(mengyan.name) and (data.card.name == "peach" or data.card.name == "god_salvation")
  end,
  on_use = function (self, event, target, player, data)
    player.room:changeMaxHp(target, 1)
  end
})

return mengyan